<?php
/**
 * 用户信息控制器
 */
class UserController extends \Our\Controller_AbstractApi {

    /**
     * 用户信息列表
     */
    public function listAction(){
        $mysql = \Mysql\UserModel::getInstance();
        $columns = array('user_id','openid','nickname','realname','headimgurl','mobile');
        $where = array();
        $order = 'subscribe_time desc';
        $limit = '10';
        $now_page = 1;
        $data  = $mysql->getPageList($where,$limit,$now_page,$order,$columns);
        echo $this->jsonEncode($this->apiData($data));
    }

    /**
     * 用户详细信息
     */
    public function infoAction(){
        $uid = $this->getRequest()->getParam("uid", 0);
        $mysql = \Mysql\UserModel::getInstance();
        $row = $mysql->getUserInfo(array('user_id'=>$uid));
        if(!empty($row)){
            echo $this->jsonEncode($this->apiData($row));
        }else{
            echo $this->jsonEncode($this->apiData('该数据不存在或者已被禁用!',1));
        }
    }

    /**
     * 用户账户变更日志列表
     */
    public function accountListAction(){

        //会员ID
        $uid = $this->getRequest()->getPost("uid", 0);
        //账户调整类型
        $type = $this->getRequest()->getPost("type",1);
        //账户增加类型
        $cat = $this->getRequest()->getPost("cat", 1);
        //当前页码
        $page = $this->getRequest()->getPost("page", 1);

        //查询条件
        $where = 'type='.$type;
        if(!empty($uid)){
            $where.=' AND user_id='.$uid;
        }else{
            echo $this->jsonEncode($this->apiData('用户不能为空!',1));
            exit;
        }
        if(!empty($cat))$where.=' AND cat='.$cat;

        $mysql = \Mysql\UserAccountModel::getInstance();
        $columns = array('type','type','remark','add_time','cat','money');
        $order = 'add_time desc';
        $limit = '10';
        $data  = $mysql->getPageList($where,$limit,$page,$order,$columns);
        echo $this->jsonEncode($this->apiData($data));
    }

    /**
     * 会员申请提现记录
     */
    public function cashListAction(){
        //会员ID
        $uid = $this->getRequest()->getParam("uid", 0);
        //申请状态
        $status = $this->getRequest()->getParam("status",3);
        //当前页码
        $page = $this->getRequest()->getParam("page", 1);

        //查询条件
        $where = 'status='.$status;
        if(!empty($uid)){
            $where.=' AND user_id='.$uid;
        }else{
            echo $this->jsonEncode($this->apiData('用户不能为空!',1));
        }

        $mysql = \Mysql\CashModel::getInstance();
        $columns = array('account','add_time','status','pay_time','note','fee');
        $order = 'add_time desc';
        $limit = '10';
        $data  = $mysql->getPageList($where,$limit,$page,$order,$columns);
        echo $this->jsonEncode($this->apiData($data));
    }

    /**
     * 个人信息表单提交
     */
    public function userInfoFormAction(){
        $uid = $this->getRequest()->getPost("uid");
        $realname = $this->getRequest()->getPost("realname",'');
        $mobile  = $this->getRequest()->getPost("mobile",'');
        $alipay  = $this->getRequest()->getPost("alipay",'');
        $up_data = array();
        if($realname) $up_data['realname'] = $realname;
        if($mobile) $up_data['mobile'] = $mobile;
        if($alipay) $up_data['alipay'] = $alipay;
        $mysql = \Mysql\UserModel::getInstance();
        $res = $mysql->update($up_data,array('user_id'=>$uid));
        if($res){
            echo $this->jsonEncode($this->apiData('ok','0'));
        }else{
            echo $this->jsonEncode($this->apiData('信息保存失败!',1));
        }
    }

    /**
     * 个人密码修改
     */
    public function userPassFormAction(){
        $uid = $this->getRequest()->getPost("uid");
        $mobile  = $this->getRequest()->getPost("mobile",'');
        $password      = $this->getRequest()->getPost("password",'');
        $password_two  = $this->getRequest()->getPost("password_two",'');

        //密码不能为空
        if(empty($password)){
            echo $this->jsonEncode($this->apiData('密码不能为空','001'));
        }

        //两次输入密码相同
        if($password!=$password_two){
            echo $this->jsonEncode($this->apiData('两次输入密码不正确!','002'));
        }
        $up_data = array();
        $mysql = \Mysql\UserModel::getInstance();
        $user_info = $mysql->getUserInfo(array('user_id'=>$uid));

        if(empty($mobile)){
            echo $this->jsonEncode($this->apiData('手机号码不能为空！','003'));
        }

        if($user_info['mobile']!=$mobile){
            echo $this->jsonEncode($this->apiData('手机号码信息不正确！','003'));
        }

        $up_data['mix'] = Hash::random(6);
        $up_data['password'] = md5(md5($password).$up_data['mix']);
        $res = $mysql->update($up_data,array('user_id'=>$uid));
        if($res){
            echo $this->jsonEncode($this->apiData('ok','0'));
        }else{
            echo $this->jsonEncode($this->apiData('密码保存失败!',1));
        }
    }
}
